package com.thrsdk.utils.local;

import android.text.TextUtils;


import com.dy.google.gson.Gson;
import com.dy.google.gson.reflect.TypeToken;
import com.thrsdk.THFSDKClient;
import com.thrsdk.utils.local.bean.BackupBean;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 业务的帮助类 主要是涉及sp的
 * @Author: Xiang Kejia
 * @Date: 2022/5/24 11:09
 */
public class SpHelper {
    private static final String KEY_PRIVACY_AGREEMENT_TIPS_SHOW = "privacy_agreement_tips_show";
    private static final String SYSTEM_PERMISSION_TIPS_SHOW = "system_permission_tips_show";//系统权限提示
    private static final String KEY_USER_BEAN = "UserBean";
    private static final String KEY_UUID = "deviceUuid"; //初始化的时候会先清空本地的uuid 不需要添加安装时间
    private static final String KEY_BACKUP_BEAN = "backupBean"; //当前网络请求的对象
    private static final String KEY_BACKUP_BEAN_LIST = "backupBeanList"; //备份全部网络地址的集合
    private static final String KEY_FLOAT_BALL_PARAMS_X = "floatBallParamsX";
    private static final String KEY_FLOAT_BALL_PARAMS_Y = "floatBallParamsY";


    public static long getPackageLastUpdateTime() {
        String name = THFSDKClient.getInstance().getContext().getPackageName();
        long time = 0;
        try {
            time = THFSDKClient.getInstance().getContext().getPackageManager().getPackageInfo(name, 0).lastUpdateTime;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return time;
    }

    /**
     * 缓存到本地当前使用的域名
     */
    public static BackupBean getCurrentBackupBean() {
        return SpUtils.getInstance().get(KEY_BACKUP_BEAN + getPackageLastUpdateTime(), BackupBean.class);
    }

    public static void setCurrentBackupBean(BackupBean backupBean) {
        SpUtils.getInstance().putSerializable(KEY_BACKUP_BEAN + getPackageLastUpdateTime(), backupBean);
    }

    /**
     * 缓存全部的备用域名
     */
    public static void setBackupBeanList(List<BackupBean> backupBeanList) {
        Gson gson = new Gson();
        String backupBeanListStr = gson.toJson(backupBeanList);
        SpUtils.getInstance().put(KEY_BACKUP_BEAN_LIST + getPackageLastUpdateTime(), backupBeanListStr);
    }

    public static List<BackupBean> getBackupBeanList() {
        List<BackupBean> backBeanList = new ArrayList<>();
        try {
            String beanListStr = (String) SpUtils.getInstance().get(KEY_BACKUP_BEAN_LIST + getPackageLastUpdateTime(), "");
            if (!TextUtils.isEmpty(beanListStr)) {
                Gson gson = new Gson();
                backBeanList = gson.fromJson(beanListStr, new TypeToken<List<BackupBean>>() {
                }.getType());
            }
        } catch (Exception e) {
//            GLog.w("SpHelper:getBackupBeanList:" + e.toString(), LogType.CatchLog);
        }
        return backBeanList;
    }


}
